<?php


class RegisterController extends BaseController
{
    var $defualtAvatar="http://localhost/web-course-backend/photo/defualt.jfif";
    public function checkPhoneNumber($phoneNumber){
        $userController = new UserController();
        $data = $userController->getList();
        $userData = $data['data'];
        for ($i=0;$i<count($userData);$i++){
            if($phoneNumber==$userData[$i]['phone_num']){
                return ReturnHelper::failed([],"该手机号已被注册");
            }
        }
        return  ReturnHelper::success([],"该手机号未被注册");
    }
    public function register($obj){
        if(VerificationController::checkVerification($obj['verificationCode'])){
            $newObj['phone_num']=$obj['phone_num'];
            $newObj['nickname']=$obj['nickname'];
            $newObj['password']=$obj['password'];
            $newObj['avatar_url']=$this->defualtAvatar;
            $newObj['is_login']=0;
            $newObj['fans_num']=0;
            $newObj['answer_num']=0;
            $userController = new UserController();
            $result=$userController->post($newObj);
            if($result['state']==200) {
                $loginController = new LoginController();
                $obj['phoneNumber']=$obj['phone_num'];
                $result=$loginController->tryLogin($obj);
                if($result['state']==200)
                    return ReturnHelper::success([], "注册成功，自动登录成功",200);
                else
                    return ReturnHelper::failed([],"注册成功，自动登录失败",201);
            }
            else
                return ReturnHelper::failed([],"该手机号已被注册",401);
        }
        else{
            return ReturnHelper::failed([],"验证码错误");
        }
    }
}